import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * DataHierarchies 数据层级配置
 * @date: 2019-8-14
 */
import { getResponse } from 'utils/utils';
import { queryIdpValue } from 'services/api';
import { queryConfig, renderTreeData, createConfig as _createConfig, // deleteConfig,
editConfig as _editConfig, getConfigDetail as _getConfigDetail } from "../services/dataHierarchiesService";
export default {
  namespace: 'dataHierarchies',
  state: {
    dataHierarchiesList: [],
    // 配置列表
    renderTree: [],
    pathMap: {},
    dataHierarchiesDetail: {},
    // 配置详情
    expandedRowKeys: []
  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CPFM.DATA_HIERARCHY.DISPLAY_STYLE');

            case 4:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  displayList: res
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 获取列表
    fetchConfigList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConfigList(_ref2, _ref3) {
      var payload, put, call, res, data, _renderTreeData, renderTree, _renderTreeData$pathM, pathMap, expandedRow;

      return _regeneratorRuntime.wrap(function fetchConfigList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              put = _ref3.put, call = _ref3.call;
              _context2.next = 4;
              return call(queryConfig, payload);

            case 4:
              res = _context2.sent;
              data = getResponse(res);
              _renderTreeData = renderTreeData(data, {}), renderTree = _renderTreeData.renderTree, _renderTreeData$pathM = _renderTreeData.pathMap, pathMap = _renderTreeData$pathM === void 0 ? {} : _renderTreeData$pathM;
              expandedRow = {
                expandedRowKeys: Object.keys(pathMap).map(function (item) {
                  return +item;
                })
              };

              if (!data) {
                _context2.next = 11;
                break;
              }

              _context2.next = 11;
              return put({
                type: 'updateState',
                payload: _objectSpread(_objectSpread({
                  dataHierarchiesList: data.content,
                  pathMap: pathMap
                }, expandedRow), {}, {
                  renderTree: renderTree
                })
              });

            case 11:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchConfigList);
    }),
    // 获取列表明细
    getConfigDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getConfigDetail(_ref4, _ref5) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function getConfigDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              put = _ref5.put, call = _ref5.call;
              _context3.next = 4;
              return call(_getConfigDetail, payload);

            case 4:
              res = _context3.sent;
              data = getResponse(res);

              if (!data) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dataHierarchiesDetail: data
                }
              });

            case 9:
              return _context3.abrupt("return", data);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, getConfigDetail);
    }),
    // 新建配置
    createConfig: /*#__PURE__*/_regeneratorRuntime.mark(function createConfig(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createConfig$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_createConfig, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createConfig);
    }),
    // 编辑配置
    editConfig: /*#__PURE__*/_regeneratorRuntime.mark(function editConfig(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function editConfig$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_editConfig, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, editConfig);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref10) {
      var payload = _ref10.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};